﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.Repositories.GPM;
using CP.Utility;
using System.IO;
using System.Collections.Specialized;

namespace CP.Controllers.GPM
{
    public class WebpartController
    {
        public IList<Webpart> GetAllWebparts()
        {
            WebpartRepository rep = new WebpartRepository();
            return rep.GetAllWebparts();
        }

        public Webpart GetWebpartByID(int id)
        {
            WebpartRepository rep = new WebpartRepository();
            return rep.GetWebpartByID(id);
        }

        public bool AddWebpart(Webpart webpart)
        {
            WebpartRepository rep = new WebpartRepository();
            Webpart w = rep.GetWebpartByFormName(webpart.form_name);
            if (w.webpart_id != 0)
                throw new Exception(webpart.form_name + "窗体名称已经存在");
            return rep.AddWebpart(webpart);
        }

        public bool AddWebpartButton(Webpart webpart, string filename)
        {
            WebpartRepository rep = new WebpartRepository();
            NameValueCollection actions = GetNameValues(filename);

            return rep.AddWebpartButton(webpart, actions);
        }

        public IList<string> AddWebpartLinkButton(Webpart webpart, string filename)
        {
            WebpartRepository rep = new WebpartRepository();
            return GetLinkButtons(filename);
        }

        private static IList<string> GetLinkButtons(string filename)
        {
            StreamReader sr = new StreamReader(filename);
            string fileContent = sr.ReadToEnd();
            IList<string> urls = new List<string>();
            Ascx_Reader.GetLinkUrls(urls, fileContent);
            return urls;
        }

        private static NameValueCollection GetNameValues(string filename)
        {
            StreamReader sr = new StreamReader(filename);
            string fileContent = sr.ReadToEnd();
            NameValueCollection actions = new NameValueCollection();
            Ascx_Reader.GetOperatorAction(actions, fileContent);
            Ascx_Reader.GetGridZoneAction(actions, fileContent);
            Ascx_Reader.GetGridViewZoneDetailsList(actions, fileContent);
            return actions;
        }

        private static NameValueCollection GetGridLists(string filename)
        {
            StreamReader sr = new StreamReader(filename);
            string fileContent = sr.ReadToEnd();
            NameValueCollection actions = new NameValueCollection();
            Ascx_Reader.GetGridZoneList(actions, fileContent);
            return actions;
        }

        public bool EditWebpart(Webpart webpart)
        {
            WebpartRepository rep = new WebpartRepository();
            Webpart w = rep.GetWebpartByFormName(webpart.form_name);
            if (w.webpart_id == 0)
                throw new Exception(webpart.form_name + "窗体名称不存在"); 
            return rep.UpdateWebpart(webpart);
        }

        public bool DeleteWebparts(IList<int> webparts)
        {
            WebpartRepository rep = new WebpartRepository();
            return rep.DeleteWebpart(webparts);
        }

        public IList<Button_List> GetButtonsByWebpartID(int webpartid)
        {
            ButtonRepository rep = new ButtonRepository();
            return rep.GetAllButtons(webpartid);
        }

        public IList<Column_List> GetListsByWebpartID(int webpartid)
        {
            ColumnListRepository rep = new ColumnListRepository();
            return rep.GetAllColumn_Lists(webpartid);
        }

        public bool AddList(int webpartid, Column_List list)
        {
            ColumnListRepository rep = new ColumnListRepository();
            return rep.AddColumn_List(webpartid, list);
        }

        public bool DeleteListByID(int webpartid, int listid)
        {
            ColumnListRepository rep = new ColumnListRepository();
            return rep.DeleteColumn_ListByID(webpartid, listid);
        }

        public bool UpdateList(int webpartid, Column_List list)
        {
            ColumnListRepository rep = new ColumnListRepository();
            return rep.UpdateColumn_List(webpartid, list);
        }

        public int GetSeqWebpartID()
        {
            WebpartRepository rep = new WebpartRepository();
            return rep.GetWebpartIDBySeq();
        }

        public bool AddButton(int webpartid, Button_List btn)
        {
            ButtonRepository rep = new ButtonRepository();
            return rep.AddButton(webpartid, btn);
        }

        public bool DeleteButtonByID(int webpartid, int btnid)
        {
            ButtonRepository rep = new ButtonRepository();
            return rep.DeleteButtonByID(webpartid, btnid);
        }

        public bool UpdateButton(int webpartid, Button_List btn)
        {
            ButtonRepository rep = new ButtonRepository();
            return rep.UpdateButton(webpartid, btn);
        }
    }
}
